/**
 * Copyright 2011 HNB. All rights reserved.
 * HNB PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package my.hnb.android.view.karaoke;

import my.hnb.android.common.ActionEvent;
import my.hnb.android.constant.EventConstant;
import my.hnb.android.constant.IntentConstants;
import my.hnb.android.controller.UserController;
import my.hnb.android.dto.SongDTO;
import my.hnb.songnhac.R;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * row thong tin mot bai nhac (ma so, ten bai nhac, cau dau tien bai nhac)
 * @author BANGHN
 */
public class KaraokeSongRow extends RelativeLayout {
	// ten bai hat
	TextView txtName;
	// ma bai hat
	TextView txtSongNumber;
	// loi bai hat
	TextView txtLyric;
	//ivFavorite
	ImageView ivFavorite;
	Context mContext;
	public KaraokeSongRow(Context context) {
		super(context);
		mContext = context;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.layout_song_row, this);
		txtLyric = (TextView) view.findViewById(R.id.txtLyric);
		txtSongNumber = (TextView) view.findViewById(R.id.txtSongNumber);
		txtName = (TextView) view.findViewById(R.id.txtName);
		ivFavorite = (ImageView) view.findViewById(R.id.ivFavorite);
	}

	/**
	 * Render thong tin bai hat
	 * @author BANGHN
	 * @param song
	 *            (Doi tuong luu thong tin bai hat)
	 */
	public void renderInfoSong(final SongDTO song, final int position) {
		txtSongNumber.setText("" + song.songNumber);
		txtLyric.setText(song.shortLyric);
		txtName.setText(song.songName);
		if(song.favorite == 1){
			ivFavorite.setImageResource(R.drawable.icon_love_active);
		}else{
			ivFavorite.setImageResource(R.drawable.icon_love);
		}
		
		ivFavorite.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				requestUpdateFavorite(song, position);
			}
		});
	}
	
	
	private void requestUpdateFavorite(SongDTO song, int position) {
		int favorite = 1 - song.favorite;
		ActionEvent e = new ActionEvent();
		Bundle b = new Bundle();
		b.putString(IntentConstants.INTENT_SONG_CODE, "" + song.songNumber);
		b.putInt(IntentConstants.INTENT_SONG_FAVORITE, favorite);
		e.viewData = b;
		e.action = EventConstant.UPDATE_FAVORITE;
		e.userData = position;
		e.sender = mContext;
		UserController.getInstance().handleViewEvent(e);
	}
}
